package com.letv.android.client.episode.fragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.ledim.activity.EpisodesActivity;
import com.ledim.activity.RoomActivity;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.LedimAlbumBean;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.bean.LedimVedioTagBean;
import com.ledim.bean.RoomAddPlayListResponse;
import com.ledim.bean.RoomInfoResponse;
import com.ledim.bean.SubmediaEpisodesResponse;
import com.ledim.bean.base.BaseListResponse;
import com.ledim.bean.base.BaseResultDataInfo;
import com.ledim.itf.LedimAlbusFragmentInterface;
import com.ledim.ledimview.VideoEpisodesView;
import com.ledim.ledimview.VideoTagsView;
import com.ledim.ledimview.VideoTitleView;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import com.letv.android.client.LetvSDK;
import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.client.constant.FragmentConstant;
import com.letv.android.client.fragment.LetvBaseFragment;
import com.letv.android.young.client.R;
import com.letv.core.bean.IVideo;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TabsFragment extends LetvBaseFragment
        implements Observer, View.OnClickListener, LedimAlbusFragmentInterface {

    public static final int LAUNCH_MODE_TOPIC = 1;
    public static final int LAUNCH_MODE_ALBUM = 2;
    public static final int LAUNCH_MODE_TV = 5;
    public static final int LAUNCH_MODE_VARITY = 11;
    public static final int LAUNCH_MODE_RECORD = 16;
    public static final int LAUNCH_MODE_SELF = 1008;
    private static final int POSITIVE = 180001;
    private static final int TV = 181031;
    private View mRootLayout;
    private ChatRoomBean mRoom;
    private VideoTitleView mTitleView;
    private VideoEpisodesView mEpisodesView;
    private VideoTagsView mTagsView;
    private TextView mAddVideoList;
    private TextView mInviteFriends;
    private TextView mDataRefresh;
    private TextView mTagNameCount;
    private EditText mTagNameEdit;
    private String mTagNameStr;

    private int mEpisodes;
    private View mButtonLayout;
    private View mAddTagLayout;
    private View mNoDataLayout;
    private int mPlayVid = 0;

    private String mMediaId;
    private String mSubMediaId;
    private String mTitle;
    private String duration;
    private AlbumPlayActivity mActivity;
    private ArrayList<LedimEpisodeBean> addEpisodes = new ArrayList<LedimEpisodeBean>(); // 剧集列表

    private boolean mInvite = false;
    private LedimAlbumBean ledimAlbumBean;
    private ArrayList<LedimVedioTagBean> tagBeanArrayList;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public static TabsFragment getInstance(String title, String duration, String mediaId,
                                           String subMediaId) {
        TabsFragment tabsFragment = new TabsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ConstantUtil.TITLE, title);
        bundle.putString(ConstantUtil.MEDIAID, mediaId);
        bundle.putString(ConstantUtil.DURATION, duration);
        bundle.putString(ConstantUtil.SUBMEDIAID, subMediaId);
        tabsFragment.setArguments(bundle);
        return tabsFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTitle = bundle.getString(ConstantUtil.TITLE);
            mMediaId = bundle.getString(ConstantUtil.MEDIAID);
            mSubMediaId = bundle.getString(ConstantUtil.SUBMEDIAID);
            duration = bundle.getString(ConstantUtil.DURATION);
            mPlayVid = Integer.valueOf(mSubMediaId);
        }
    }

    public void setActivity(AlbumPlayActivity activity) {
        this.mActivity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootLayout = inflater.inflate(R.layout.letv_detailplay_album_lower_playerlibs, null);
        initView(mRootLayout);
        EventBus.getDefault().register(this);
        return mRootLayout;
    }

    /**
     * 初始化界面
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void initView(View mRootLayout) {
        mAddVideoList = (TextView) mRootLayout.findViewById(R.id.video_detail_add_list);
        mInviteFriends = (TextView) mRootLayout.findViewById(R.id.video_detail_invite);
        mTagNameCount = (TextView) mRootLayout.findViewById(R.id.add_tags_count);
        mTagNameEdit = (EditText) mRootLayout.findViewById(R.id.add_tags_name);
        mButtonLayout = mRootLayout.findViewById(R.id.button_layout);
        mAddTagLayout = mRootLayout.findViewById(R.id.add_tags_layout);
        mNoDataLayout = mRootLayout.findViewById(R.id.no_data);
        mDataRefresh = (TextView) mRootLayout.findViewById(R.id.data_refresh);
        mTitleView = (VideoTitleView) mRootLayout.findViewById(R.id.video_title_view);
        mEpisodesView = (VideoEpisodesView) mRootLayout.findViewById(R.id.video_episodes_view);
        mTagsView = (VideoTagsView) mRootLayout.findViewById(R.id.video_tags_view);

        mAddVideoList.setOnClickListener(this);
        mInviteFriends.setOnClickListener(this);
        mDataRefresh.setOnClickListener(this);
        mEpisodesView.setActivity(mActivity);

        if (TextUtils.isEmpty(mMediaId) || mMediaId.equals("0")) {
            //没有剧集信息
            mTitleView.bindDataZero(mTitle, mSubMediaId);
            mTitleView.setVisibility(View.VISIBLE);
            mTagsView.setVisibility(View.VISIBLE);
            mButtonLayout.setVisibility(View.VISIBLE);
        } else {
            getMediaDetail(mMediaId);
        }

        //输入的标签文字
        mTagNameEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mTagNameStr = mTagNameEdit.getText().toString().trim();
                mTagNameCount.setText((10 - mTagNameStr.length()) + "");
            }
        });
        //点击发送完成创建
        mTagNameEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    if (mTagNameStr == null || mTagNameStr.length() == 0) {
                        ToastUtil.toast("请输入标签");
                    } else {
                        if (tagBeanArrayList.size() < 10) {
                            mTagsView.createTags(mTagNameStr, mMediaId, mSubMediaId);
                        } else {
                            ToastUtil.toast("标签已满，下次请早！");
                        }
                    }
                    return true;
                }
                return false;
            }

        });

    }

    @Subscribe
    public void onEvent(Message event) {
        Message msg = event;
        if (msg.what == ConstantUtil.EventBusMsgConstant.BASE_MODEL_EPISODE_FAIL) {
            //剧集拉取失败显示专辑简介,vid=0的情况，音乐
            mTitleView.setVisibility(View.VISIBLE);
        }

    }

    public LedimEpisodeBean getCurrentEpisode() {
        return mEpisodesView.mCurrentEpisode;
    }

    @Override
    public String getTagName() {
        return FragmentConstant.TAG_FRAGMENT_ALBUM_TAB;
    }

    @Override
    public int getContainerId() {
        return R.id.play_album_tab_contain;
    }

    /**
     * 关闭键盘
     */
    public void closeKeyBoard() {
        mTagNameEdit.clearFocus();
        InputMethodManager imm =
                (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mTagNameEdit.getWindowToken(), 0);
    }

    /**
     * 播放上一集
     *
     * @param vid
     *         当前播放id
     */
    public void refreshLast(int vid) {
        mSubMediaId = vid + "";
        if (submediaResponse.data != null && submediaResponse.data.size() > 0) {
            for (int i = 0; i < submediaResponse.data.size(); i++) {
                LedimEpisodeBean episode = submediaResponse.data.get(i);
                if (episode.id == vid) {
                    if (i > 0) {
                        mActivity.setVid(submediaResponse.data.get(i - 1).id, 0);
                        mEpisodesView.reFreshEpisode(submediaResponse.data.get(
                                i - 1).id, submediaResponse.data);
                        addEpisodes.clear();
                        addEpisodes.add(submediaResponse.data.get(i - 1));
                    } else {
                        submediaResponse.data.get(0);
                        mActivity.setVid(submediaResponse.data.get(0).id, 0);
                        mEpisodesView.reFreshEpisode(submediaResponse.data.get(0).id, submediaResponse.data);
                        addEpisodes.clear();
                        addEpisodes.add(submediaResponse.data.get(0));
                    }
                    return;
                }
            }
            mActivity.setVid(submediaResponse.data.get(0).id, 0);
            mEpisodesView.reFreshEpisode(submediaResponse.data.get(0).id, submediaResponse.data);
            addEpisodes.clear();
            addEpisodes.add(submediaResponse.data.get(0));
        }
    }

    private boolean isNext = false;

    /**
     * 播放下一集
     *
     * @param vid
     *         当前播放id
     */
    public void refreshNext(int vid) {
        LogUtil.e("refreshNext");
        mSubMediaId = vid + "";
        if (submediaResponse != null && submediaResponse.data != null &&
                submediaResponse.data.size() > 0) {
            for (int i = 0; i < submediaResponse.data.size(); i++) {
                LedimEpisodeBean episode = submediaResponse.data.get(i);
                if (episode.id == vid) {
                    if (i < submediaResponse.data.size() - 1) {
                        mActivity.setVid(submediaResponse.data.get(i + 1).id, 0);
                        mEpisodesView.reFreshEpisode(submediaResponse.data.get(
                                i + 1).id, submediaResponse.data);
                        addEpisodes.clear();
                        addEpisodes.add(submediaResponse.data.get(i + 1));
                    } else {
                        getEpisodesShowNext();
                    }
                    return;
                }
            }
            mActivity.setVid(submediaResponse.data.get(0).id, 0);
            mEpisodesView.reFreshEpisode(submediaResponse.data.get(0).id, submediaResponse.data);
            addEpisodes.clear();
            addEpisodes.add(submediaResponse.data.get(0));
        } else {
            mActivity.finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_detail_add_list:
                if (!LedimSpUtils.isLogin()) {
                    //未登录
                    LedimFunctionUtils.showLoginDialog(getActivity());
                } else {
                    //添加放映单
                    addRoomPlayLists();
                }
                break;
            case R.id.video_detail_invite:
                //也可以使用添加播放协议中的ext得到放映厅状态
                if (!LedimSpUtils.isLogin()) {
                    //未登录
                    LedimFunctionUtils.showLoginDialog(getActivity());
                } else {
                    //                    //邀请好友，进入放映厅
                    //                    if (mRoom.status==1){
                    //                        IVideo roomVideo = new IVideo();
                    //                        roomVideo.setmVid(0);
                    //                        LetvSDK.getInstance().playRoom(mContext, roomVideo, mRoom, true);
                    //                    } else if (mRoom.status==0){
                    //                        Intent intent = new Intent(getActivity(), RoomActivity.class);
                    //                        startActivity(intent);
                    //                        getActivity().overridePendingTransition(R.mUserIconAnimator.push_right_in, R.mUserIconAnimator.push_right_out);
                    //                    }
                    mInvite = true;
                    //添加放映单
                    addRoomPlayLists();
                }
                break;
            case R.id.data_refresh:
                getMediaDetail(mMediaId);
                getAllTagsLists();
                break;
        }
    }

    private void addRoomPlayLists() {
        if (mRoom != null) {
            if (!mMediaId.equals("0")) {
                if (addEpisodes.size() > 0) {
                    LedimApiServiceUtils.addRoomPlayList(mRoom.roomId, addEpisodes, observer);
                }
            } else {
                //相关视频
                LedimApiServiceUtils.addRoomPlayListByVid(mRoom.roomId, mMediaId, mSubMediaId, mTitle, duration, observer);
            }
        }
    }

    private void getAllTagsLists() {
        if (TextUtils.isEmpty(mMediaId) || mMediaId.equals("0")) {
            getTagsList(mSubMediaId);
        } else {
            getTagsList(mMediaId);
        }
    }

    //得到剧集信息显示
    private void getEpisodesShow() {
        isNext = false;
        //        switch (mMediasTypeMediaGetModel.data.varietyShow) {
        //            case LAUNCH_MODE_TOPIC://栏目1
        //                mMediasTypeMediaSub_mediaEpisodesGetModel.getMediaLists(this, mMediaId,mSubMediaId,10,true,0,false, 2);
        //                break;
        //            default:
        if (ledimAlbumBean.albumType != null && !ledimAlbumBean.albumType.equals("null")) {
            switch (ledimAlbumBean.getalbumType()) {
                case POSITIVE://正片
                case TV://TV版
                    switch (ledimAlbumBean.getCategory()) {
                        case LAUNCH_MODE_ALBUM://电视剧、动漫2,5    得到升序剧集列表
                        case LAUNCH_MODE_TV:
                        case LAUNCH_MODE_RECORD://记录片16
                        case LAUNCH_MODE_TOPIC://电影1
                        case LAUNCH_MODE_SELF://自制1008
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 0, 2, 0, mediaListObserver);
                            break;
                        case LAUNCH_MODE_VARITY://综艺11    得到降序列表
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 2, 0, mediaListObserver);
                            break;
                        default://音乐其它
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 1, 0, mediaListObserver);
                            break;
                    }
                    break;
                default:
                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 1, 0, mediaListObserver);
                    break;
            }
        } else {
            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 1, 0, mediaListObserver);
        }
        //                break;
        //        }
    }

    private SubmediaEpisodesResponse submediaResponse;
    private LedimHttpObserver mediaListObserver =
            new LedimHttpObserver<SubmediaEpisodesResponse>() {
                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }

                @Override
                public void onNext(SubmediaEpisodesResponse response) {
                    super.onNext(response);
                    submediaResponse = response;
                    if (response != null && response.success) {
                        if (!isNext) {
                            mEpisodesPage = response.ext.pagenum;
                        }
                        if (response.ext.totalNum <= mEpisodesPage * 10) {
                            mEpisodesPage = 0;
                        }
                        if (isNext) {
                            mPlayVid = response.data.get(0).id;
                            mActivity.setVid(response.data.get(0).id, 0);
                            mEpisodesView.reFreshEpisode(response.data.get(0).id, response.data);
                            addEpisodes.clear();
                            addEpisodes.add(response.data.get(0));
                        } else {
                            //link中的vid是0的情况
                            if (mSubMediaId.length() > 0 && mSubMediaId.equals("0")) {
                                mPlayVid = response.data.get(0).id;
                                mActivity.setVid(response.data.get(0).id, 0);
                                addEpisodes.clear();
                                addEpisodes.add(response.data.get(0));

                            } else {
                                addEpisodes.clear();
                                for (int i = 0; i < response.data.size(); i++) {
                                    if (response.data.get(i).id == mPlayVid) {
                                        addEpisodes.add(response.data.get(i));
                                    }
                                }
                            }
                        }
                        mEpisodesView.bindData(mPlayVid, submediaResponse.data, ledimAlbumBean, TabsFragment.this);
                        mTagsView.setVisibility(View.VISIBLE);
                        mTitleView.setVisibility(View.VISIBLE);
                        mEpisodesView.setVisibility(View.VISIBLE);
                        mButtonLayout.setVisibility(View.VISIBLE);
                        mActivity.setData(submediaResponse.data);

                        //mListView.setOnTouchListener(new View.OnTouchListener() {
                        //    @Override
                        //    public boolean onTouch(View v, MotionEvent event) {
                        //        closeKeyBoard();
                        //        mAddTagLayout.setVisibility(View.GONE);
                        //        mButtonLayout.setVisibility(View.VISIBLE);
                        //        return false;
                        //    }
                        //});

                    } else {
                        if (isNext) {

                        }
                        // mEpisodesView.setVisibility(View.GONE);
                    }
                }

            };

    private int mEpisodesPage = 1;

    //得到下一页的剧集信息显示
    private void getEpisodesShowNext() {
        isNext = true;
        //        switch (mMediasTypeMediaGetModel.data.varietyShow) {
        //            case LAUNCH_MODE_TOPIC://栏目1
        //                mMediasTypeMediaSub_mediaEpisodesGetModel.getMediaListsNext(this, mMediaId, mSubMediaId, 10, true, 0, false, 2);
        //                break;
        //            default:
        if (ledimAlbumBean.albumType != null) {
            switch (ledimAlbumBean.getalbumType()) {
                case POSITIVE://正片
                case TV://TV版
                    switch (ledimAlbumBean.getCategory()) {
                        case LAUNCH_MODE_ALBUM://电视剧、动漫2,5    得到升序剧集列表
                        case LAUNCH_MODE_TV:
                        case LAUNCH_MODE_RECORD://记录片16
                        case LAUNCH_MODE_TOPIC://电影1
                        case LAUNCH_MODE_SELF://自制1008
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 0, 2, ++mEpisodesPage, mediaListObserver);
                            break;
                        case LAUNCH_MODE_VARITY://综艺11    得到降序列表
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 2, ++mEpisodesPage, mediaListObserver);
                            break;
                        default://音乐其它
                            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 1, ++mEpisodesPage, mediaListObserver);
                            break;
                    }
                    break;
                default:
                    LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 1, ++mEpisodesPage, mediaListObserver);
                    break;
            }
        } else {
            LedimApiServiceUtils.getMediaLists(mMediaId, mSubMediaId, 10, 1, 1, 1, mediaListObserver);
        }
        //                break;
        //        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (LedimSpUtils.isLogin()) {
            LedimApiServiceUtils.getMyRoomInfo(new LedimHttpObserver<RoomInfoResponse>() {
                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }

                @Override
                public void onNext(RoomInfoResponse response) {
                    super.onNext(response);
                    if (response.success) {
                        mRoom = response.data;
                    }
                }
            });
        }
        getAllTagsLists();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 打开键盘
     */
    private void openKeyBoard() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               InputMethodManager inputManager = (InputMethodManager) mTagNameEdit.getContext()
                                                                                                  .getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(mTagNameEdit, 0);
                           }
                       },

                100);
    }

    @Override
    public void showAlbus(int episodes, int playVid) {
        mEpisodes = episodes;
        mPlayVid = playVid;
        Intent intent = new Intent(getActivity(), EpisodesActivity.class);
        intent.putExtra(EpisodesActivity.MEDIAID, mMediaId);
        intent.putExtra(EpisodesActivity.EPISODES, mEpisodes);
        intent.putExtra(EpisodesActivity.SUBMEDIAID, mSubMediaId);
        intent.putExtra(EpisodesActivity.ALBUM, ledimAlbumBean);
        startActivityForResult(intent, 1);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            int playId = data.getIntExtra(ConstantUtil.ID, -1);
            mSubMediaId = playId + "";
            mPlayVid = playId;
            getEpisodesShow();
            mActivity.setVid(playId, 0);
        }
    }

    @Override
    public void onAddTagsClick() {
        mTagNameEdit.requestFocus();
        mAddTagLayout.setVisibility(View.VISIBLE);
        mButtonLayout.setVisibility(View.GONE);
        openKeyBoard();
    }

    @Override
    public void closeAddTagsView() {
        //创建完标签后关闭标签框
        mAddTagLayout.setVisibility(View.GONE);
        mButtonLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void addPlayListEpisode(LedimEpisodeBean bean) {
        addEpisodes.clear();
        addEpisodes.add(bean);
    }

    @Override
    public void setDescription(LedimEpisodeBean episode) {
        mTitleView.setDescription(episode);
    }

    /**
     * 获取媒资详情
     */
    private void getMediaDetail(String mediaId) {
        LedimApiServiceFactory.createService(ApiService.class).getMediaDetail(mediaId)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseResultDataInfo<LedimAlbumBean>>() {
                                  @Override
                                  public void onCompleted() {
                                      super.onCompleted();
                                  }

                                  @Override
                                  public void onError(Throwable e) {
                                      super.onError(e);
                                      LogUtil.e(e.getMessage());
                                      mNoDataLayout.setVisibility(View.VISIBLE);
                                  }

                                  @Override
                                  public void onNext(BaseResultDataInfo<LedimAlbumBean> data) {
                                      super.onNext(data);
                                      if (data != null) {
                                          mNoDataLayout.setVisibility(View.GONE);
                                          if (data.success) {
                                              //成功
                                              if (data.data != null) {
                                                  ledimAlbumBean = data.data;
                                                  mTitleView.bindData(data.data);
                                                  getEpisodesShow();
                                              }
                                          } else {
                                              ToastUtil.toast(data.message);
                                          }
                                      } else {
                                          mNoDataLayout.setVisibility(View.VISIBLE);
                                      }
                                  }
                              });
    }

    /**
     * 获取tag列表数据
     *
     * @param id
     */
    private void getTagsList(String id) {
        LedimApiServiceFactory.createService(ApiService.class)
                              .getTagsList(id)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseListResponse<LedimVedioTagBean>>() {
                                  @Override
                                  public void onCompleted() {
                                      super.onCompleted();
                                  }

                                  @Override
                                  public void onError(Throwable e) {
                                      super.onError(e);
                                      LogUtil.e(e.getMessage());
                                  }

                                  @Override
                                  public void onNext(BaseListResponse<LedimVedioTagBean> data) {
                                      super.onNext(data);
                                      if (data != null) {
                                          if (data.success) {
                                              //成功
                                              mTagsView.setVisibility(View.VISIBLE);
                                              tagBeanArrayList =
                                                      (ArrayList<LedimVedioTagBean>) data.data;
                                              mTagsView.bindData((ArrayList<LedimVedioTagBean>) data.data, TabsFragment.this);
                                          } else {
                                              ToastUtil.toast(data.message);
                                          }
                                      }
                                  }
                              });

    }

    private LedimHttpObserver observer = new LedimHttpObserver<RoomAddPlayListResponse>() {
        @Override
        public void onError(Throwable e) {
            super.onError(e);
        }

        @Override
        public void onNext(RoomAddPlayListResponse response) {
            super.onNext(response);
            if (response != null && response.success) {
                if (mInvite) {
                    //邀请好友，进入放映厅
                    mActivity.finish();
                    if (mRoom.status == 1) {
                        IVideo roomVideo = new IVideo();
                        roomVideo.setmVid(0);
                        LetvSDK.getInstance().playRoom(mContext, roomVideo, mRoom, true, false);
                    } else if (mRoom.status == 0) {
                        Intent intent = new Intent(getActivity(), RoomActivity.class);
                        intent.putExtra(RoomActivity.ROOM_ID, mRoom);
                        intent.putExtra(RoomActivity.MY_ROOM_FLAG, true);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
                    }
                    mInvite = false;
                } else {
                    ToastUtil.toast("好了，大王！");
                }
            }

        }
    };

    @Override
    public void update(Observable observable, Object data) {

    }
}
